package com.bazaaroid.client.web.gwt.partnermodule.client.view.locations;

import com.bazaaroid.client.web.gwt.partnermodule.client.dto.LocationDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.TitleCommandBar;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.locations.impl.LocationsTabViewImpl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditLocationEntryView extends Composite {

	private LocationsTabViewImpl locationsTabView;
	private LocationDTO locationDTO;
	private LocationEntryView locationEntryView;
	private TitleCommandBar titleBar = new TitleCommandBar("New Location");
	private TextBox nameTextBox = new TextBox();
	private TextBox latitudeTextBox = new TextBox();
	private TextBox longitudeTextBox = new TextBox();
	
	private class SaveClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			locationDTO.setName(nameTextBox.getText());
			locationDTO.setLatitude(latitudeTextBox.getText());
			locationDTO.setLongitude(longitudeTextBox.getText());
			locationsTabView.onLocationEntrySaved(locationEntryView, EditLocationEntryView.this, locationDTO);
		}
	}
	
	private class CancelClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			if(locationEntryView != null) 
				locationEntryView.getLocationsTabView().endEdit(EditLocationEntryView.this, locationEntryView);
			else 
				removeFromParent();
		}
	}
	
	public EditLocationEntryView(LocationsTabViewImpl locationsTabView, LocationDTO locationDTO) {
		this.locationsTabView = locationsTabView;
		this.locationDTO = locationDTO;
		
		VerticalPanel editLocationPanel = new VerticalPanel();
		initWidget(editLocationPanel);
		
		titleBar.addCommand("Save", new SaveClickHandler());
		titleBar.addCommand("Cancel", new CancelClickHandler());
		editLocationPanel.add(titleBar);
		
		editLocationPanel.add(new Label("Name"));
		editLocationPanel.add(nameTextBox);
		editLocationPanel.add(new Label("Latitude"));
		editLocationPanel.add(latitudeTextBox);
		editLocationPanel.add(new Label("Longitude"));
		editLocationPanel.add(longitudeTextBox);
		editLocationPanel.setStyleName("locationPanel");
	}
	
	public EditLocationEntryView(LocationEntryView locationEntryView) {
		this(locationEntryView.getLocationsTabView(),locationEntryView.getLocationDTO());
		this.locationEntryView = locationEntryView;
		
		titleBar.setTitle("Edit Location");
		nameTextBox.setText(locationDTO.getName());
		latitudeTextBox.setText(locationDTO.getLatitude());
		longitudeTextBox.setText(locationDTO.getLongitude());
	}

}
